MemoryStream zahada ...
Otázka od: tomsir1.edu@mail.cez.cz
27. 5. 2004 3:26
Ahoj,
mam nasledujici kod :
type
TZaznam = record
x , y : integer;
end;
procedure TForm1.Button1Click(Sender: TObject);
var i : integer;
zaznam : TZaznam;
begin
RichEdit1.Clear;
ms := TMemoryStream.Create;
// zapis do memory streamu
for i := 0 to 49 do
begin
Zaznam.x := i;
Zaznam.y := i;
ms.Write(Zaznam,SizeOf(TZaznam));
// ms.Seek(SizeOf(TZaznam),soFromCurrent);
end;
// cteni z memory streamu
ms.Seek(0,soFromBeginning);
while ms.position < ms.Size do
begin
ms.Read(Zaznam, SizeOf(Zaznam));
RichEdit1.Lines.Add(IntToStr(Zaznam.x)+':'+IntToStr(Zaznam.y));
ms.Seek(SizeOf(TZaznam),soFromCurrent);
end;
end;
ocekaval bych, ze mi to do richeditu vypise :
0:0
1:1
2:2
3:3
....
49:49
nicmene mi to vypise kazdy druhy zaznam tj. 0,2,4,6 ....48
Kdyz odkomentuji ten radek se seek v cyklu pro zapis, je vse v poradku.
Help hovori takto :
Writes Count bytes from Buffer to the current position in the memory buffer
and updates the current position by Count bytes.
Description:
Use Write to insert Count bytes into the memory buffer of the memory
stream, starting at the current position. Write will increase the size of
the memory buffer, if necessary, to accommodate the data being written in.
If the current position is not the end of the memory buffer, Write will
overwrite the data following the current position.
Write updates the Size property to Position + Count, and sets the Position
property to the new value of Size. Thus, any data that was stored in the
memory stream in the Count bytes after the current position is lost when
calling Write.
Pripada mi to, jako by neprobehlo to prestaveni ukazatele pozice ve streamu
a velikosti streamu. Delam nejakou chybu ??
Diky za pomoc
Radek
Odpovedá: Pave Gazda
27. 5. 2004 6:25
Ahoj,
zkus to takto: (tj. bez prikazu seek)
> for i := 0 to 49 do
> begin
> Zaznam.x := i;
> Zaznam.y := i;
> ms.Write(Zaznam,SizeOf(TZaznam));
> end;
>
> ms.Seek(0,soFromBeginning);
> while ms.position < ms.Size do
> begin
> ms.Read(Zaznam, SizeOf(Zaznam));
> RichEdit1.Lines.Add(IntToStr(Zaznam.x)+':'+IntToStr(Zaznam.y));
> end;
Pavel
Odpovedá: Karel Rys
27. 5. 2004 6:30
tomsir1.edu@mail.cez.cz dne 27 May 2004 v 4:10:
> ms.Seek(0,soFromBeginning);
> while ms.position < ms.Size do
> begin
> ms.Read(Zaznam, SizeOf(Zaznam));
> RichEdit1.Lines.Add(IntToStr(Zaznam.x)+':'+IntToStr(Zaznam.y));
> ms.Seek(SizeOf(TZaznam),soFromCurrent);
> end;
"ms.Seek(SizeOf(TZaznam),soFromCurrent)" neni potreba, protoze pri cteni,
stejne jako pri zapisu,
se pozice posune sama o pocet prectenych/zapsanych bajtu.
Karel Rys
Odpovedá: Vlko II.
27. 5. 2004 13:46
On Thu, 27 May 2004 04:10:21 +0200, <tomsir1.edu@mail.cez.cz> wrote:
> // cteni z memory streamu
> ms.Seek(0,soFromBeginning);
> while ms.position < ms.Size do
> begin
> ms.Read(Zaznam, SizeOf(Zaznam));
> RichEdit1.Lines.Add(IntToStr(Zaznam.x)+':'+IntToStr(Zaznam.y));
> ms.Seek(SizeOf(TZaznam),soFromCurrent);
> end;
> end;
chyba je v seek. pri read, sa posunie pozicia suboru a ty ju seek, este
posunies o jeden zaznam.
vlko
--
--------------
mail: vlko_@pobox.sk
web: http://vlko.zilina.net